package org.example;


public class Main34 {
    public static void main(String[] args) {
        System.out.println("测试1");
        String str = "([)]";
        if(isMatch(str))
            System.out.println(str + "中括号是匹配的");
        else
            System.out.println(str + "中括号不是匹配的");
        System.out.println("测试2");
        str = "([])";
        if(isMatch(str))
            System.out.println(str + "中括号是匹配的");
        else
            System.out.println(str + "中括号不是匹配的");
    }


    public static boolean isMatch(String str) {
        int i = 0;
        char e, x;
        SqStackClass<Character> st = new SqStackClass<Character>();
        while (i < str.length()) {
            e = str.charAt(i);
            if (e == '(' || e == '[' || e == '{')
                st.push(e);
            else {
                if (e == ')') {
                    if (st.empty())
                        return false;
                    if (st.peek() != ')')
                        return false;
                    st.pop();
                }
                if (e == ']') {
                    if (st.empty())
                        return false;
                    if (st.peek() != ']')
                        return false;
                    st.pop();
                }
                if (e == '}') {
                    if (st.empty())
                        return false;
                    if (st.peek() != '}')
                        return false;
                    st.pop();
                }
            }
            i++;
        }
        if(st.empty())
            return true;
        else
            return false;
    }
}